#include "opcodes.hpp"

using namespace rep;

namespace vm{

  Slot* Ref::dereference(Slot* env){
    
    switch(kind){
    case LOCAL:
      if(parts.a == 0){
        return FrameHelper::immDirectLookup(env, parts.b);
      }else{
        return FrameHelper::immAncestralLookup(env, parts.a, parts.b);
      }
      break;
    case INDIRECT:
    case UP_IND:
    case IND_UP:
      
    case TOPLEVEL:
      //unhandled
    default:
      printf("ARG, can't dereference ref\n");
      exit(1);
      return NULL;
    }
  }

  Slot Ref::value(Slot* env){
    Slot ans;
    switch(kind){
    case UNSIGNED:
      ans.tag = Slot::UINT;
      ans.u = un;
      return ans;
      break;
    case SIGNED:
      ans.tag = Slot::INT;
      ans.i = sig;
      return ans;
      break;
    default:
      return *dereference(env);
      break;
    }
  }
};
